<div class="m-bg">
<h1>SQL UPDATE 语句</h1>
<p class="intro">UPDATE 语句用于更新表中的记录。</p> <h2>SQL UPDATE 语句</h2> <p>UPDATE 语句用于更新表中已存在的记录。</p> <h3>SQL UPDATE 语法</h3> <div class="code notranslate"><pre><code class="language-sql"><div> UPDATE <em>table_name</em><br/> SET <em>column1</em>=<em>value1</em>,<em>column2</em>=<em>value2</em>,...<br/> WHERE <em>some_column</em>=<em>some_value</em>;</div></code></pre></div> <br/><table class="lamp"><tr>
<th width="34"><span aria-hidden="true" class="g-bg glyphicon glyphicon-flag margin-l-5"></span></th> <td> <strong>请注意 SQL UPDATE 语句中的 WHERE 子句！</strong><br/> WHERE 子句规定哪条记录或者哪些记录需要更新。如果您省略了 WHERE 子句，所有的记录都将被更新！</td> </tr></table>
<h2>演示数据库</h2> <p>在本教程中，我们将使用众所周知的 Northwind 样本数据库。</p> <p>下面是选自 "Customers" 表的数据：</p> <table class="reference notranslate">
<tr>
<th>CustomerID</th> <th>CustomerName</th> <th>ContactName</th> <th>Address</th> <th>City</th> <th>PostalCode</th> <th>Country</th> </tr>
<tr>
<td>1<br/><br/>
</td> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Obere Str. 57</td> <td>Berlin</td> <td>12209</td> <td>Germany</td> </tr>
<tr>
<td>2</td> <td>Ana Trujillo Emparedados y helados</td> <td>Ana Trujillo</td> <td>Avda. de la Constitución 2222</td> <td>México D.F.</td> <td>05021</td> <td>Mexico</td> </tr>
<tr>
<td>3</td> <td>Antonio Moreno Taquería</td> <td>Antonio Moreno</td> <td>Mataderos 2312</td> <td>México D.F.</td> <td>05023</td> <td>Mexico</td> </tr>
<tr>
<td>4<br/><br/>
</td> <td>Around the Horn</td> <td>Thomas Hardy</td> <td>120 Hanover Sq.</td> <td>London</td> <td>WA1 1DP</td> <td>UK</td> </tr>
<tr>
<td>5</td> <td>Berglunds snabbköp</td> <td>Christina Berglund</td> <td>Berguvsvägen 8</td> <td>Luleå</td> <td>S-958 22</td> <td>Sweden</td> </tr>
</table>
<h2>SQL UPDATE 实例</h2> <p>假设我们要把客户 "Alfreds Futterkiste" 更新为一个新的联系人和城市。</p> <p>我们使用下面的 SQL 语句：</p> <div class="example margin-b-10"> <h2 class="example">实例</h2> <pre><code class="language-sql"><div class="example_code notranslate"> UPDATE Customers<br/> SET ContactName='Alfred Schmidt', City='Hamburg'<br/> WHERE CustomerName='Alfreds Futterkiste'; </div></code></pre> </div> <p>现在，选自 "Customers" 表的数据如下所示：</p> <table class="reference notranslate">
<tr>
<th>CustomerID</th> <th>CustomerName</th> <th>ContactName</th> <th>Address</th> <th>City</th> <th>PostalCode</th> <th>Country</th> </tr>
<tr>
<td>1<br/><br/>
</td> <td>Alfreds Futterkiste</td> <td>Alfred Schmidt</td> <td>Obere Str. 57</td> <td>Hamburg</td> <td>12209</td> <td>Germany</td> </tr>
<tr>
<td>2</td> <td>Ana Trujillo Emparedados y helados</td> <td>Ana Trujillo</td> <td>Avda. de la Constitución 2222</td> <td>México D.F.</td> <td>05021</td> <td>Mexico</td> </tr>
<tr>
<td>3</td> <td>Antonio Moreno Taquería</td> <td>Antonio Moreno</td> <td>Mataderos 2312</td> <td>México D.F.</td> <td>05023</td> <td>Mexico</td> </tr>
<tr>
<td>4<br/><br/>
</td> <td>Around the Horn</td> <td>Thomas Hardy</td> <td>120 Hanover Sq.</td> <td>London</td> <td>WA1 1DP</td> <td>UK</td> </tr>
<tr>
<td>5</td> <td>Berglunds snabbköp</td> <td>Christina Berglund</td> <td>Berguvsvägen 8</td> <td>Luleå</td> <td>S-958 22</td> <td>Sweden</td> </tr>
</table>
<h2>Update 警告！</h2> <p>在更新记录时要格外小心！在上面的实例中，如果我们省略了 WHERE 子句，如下所示：</p> <div class="code notranslate"><pre><code class="language-sql"><div> UPDATE Customers<br/> SET ContactName='Alfred Schmidt', City='Hamburg'; </div></code></pre></div> <p>"Customers" 表将如下所示：</p> <table class="reference notranslate">
<tr>
<th>CustomerID</th> <th>CustomerName</th> <th>ContactName</th> <th>Address</th> <th>City</th> <th>PostalCode</th> <th>Country</th> </tr>
<tr>
<td>1<br/><br/>
</td> <td>Alfreds Futterkiste</td> <td>Alfred Schmidt</td> <td>Obere Str. 57</td> <td>Hamburg</td> <td>12209</td> <td>Germany</td> </tr>
<tr>
<td>2</td> <td>Ana Trujillo Emparedados y helados</td> <td>Alfred Schmidt</td> <td>Avda. de la Constitución 2222</td> <td>Hamburg</td> <td>05021</td> <td>Mexico</td> </tr>
<tr>
<td>3</td> <td>Antonio Moreno Taquería</td> <td>Alfred Schmidt</td> <td>Mataderos 2312</td> <td>Hamburg</td> <td>05023</td> <td>Mexico</td> </tr>
<tr>
<td>4<br/><br/>
</td> <td>Around the Horn</td> <td>Alfred Schmidt</td> <td>120 Hanover Sq.</td> <td>Hamburg</td> <td>WA1 1DP</td> <td>UK</td> </tr>
<tr>
<td>5</td> <td>Berglunds snabbköp</td> <td>Alfred Schmidt</td> <td>Berguvsvägen 8</td> <td>Hamburg</td> <td>S-958 22</td> <td>Sweden</td> </tr>
</table>
<div class="text-center padding-10 margin-t-5">
</div>
</div>